package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemBusinessType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.req.OperationByIdReq;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.BConst;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataBusinessTypeDTO;
import com.ctshk.rpc.system.dto.MainDataManualSystemDTO;
import com.ctshk.rpc.system.entity.MainDataManualSystem;
import com.ctshk.rpc.system.mapper.MainDataManualSystemMapper;
import com.ctshk.rpc.system.req.MainDataManualSystemCreateReq;
import com.ctshk.rpc.system.req.MainDataManualSystemQueryReq;
import com.ctshk.rpc.system.req.MainDataManualSystemUpdateReq;
import com.ctshk.rpc.system.service.IMainDataManualSystemService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 外挂系统主数据service
 *
 * @author Qiaoxin.Hong
 */
@DubboService
public class MainDataManualSystemServiceImpl implements IMainDataManualSystemService, BaseService {

    @Autowired
    MainDataManualSystemMapper mainDataManualSystemMapper;

    /**
     * 创建外挂系统主数据
     *
     * @param req
     * @return
     */
    @Transactional
    @Override
    public Result<Long> createData(MainDataManualSystemCreateReq req, TokenUser tokenUser) {
        MainDataManualSystem entity = newCreateEntity(MainDataManualSystem.class, req, tokenUser);
        entity.setStatus(BConst.YES);
        mainDataManualSystemMapper.insert(entity);
        return newResult(entity.getId());
    }

    /**
     * 修改外挂系统主数据
     *
     * @param req
     * @return
     */
    @Transactional
    @Override
    public Result<Void> updateData(MainDataManualSystemUpdateReq req, TokenUser tokenUser) {
        MainDataManualSystem entity = newUpdateEntity(MainDataManualSystem.class, req, tokenUser);
        mainDataManualSystemMapper.updateById(entity);
        return newResult();
    }

    /**
     * 删除外挂系统主数据
     *
     * @param req
     * @return
     */
    @Transactional
    @Override
    public Result<Void> deleteData(OperationByIdReq req, TokenUser tokenUser) {
//		MainDataManualSystem entity = newDeleteEntity(MainDataManualSystem.class, req.getId(), tokenUser);
//		mainDataManualSystemMapper.updateById(entity);
        mainDataManualSystemMapper.deleteById(req.getId());
        return newResult();
    }

    /**
     * 启用/禁用外挂系统主数据
     *
     * @param req
     * @return
     */
    @Transactional
    @Override
    public Result<Void> changeStatus(OperationByIdReq req, TokenUser tokenUser) {
        MainDataManualSystem entity = mainDataManualSystemMapper.assertExistById(req.getId());
        //有效->无效
        if (!BConst.ZERO.equals(entity.getStatus())) {
            entity.setStatus(BConst.ZERO);
        } else {  //无效->有效
            entity.setStatus(BConst.ONE);
        }
        fillUpdateEntity(entity);
        mainDataManualSystemMapper.updateById(entity);
        return newResult();
    }

    /**
     * 查询外挂系统主数据列表
     *
     * @param req
     * @return
     */
    @Override
    public Result<PageResponse<MainDataManualSystemDTO>> queryList(MainDataManualSystemQueryReq req) {
        QueryWrapper<MainDataManualSystem> qw = new QueryWrapper<>();
        qw.lambda().eq(req.getType() != null, MainDataManualSystem::getType, req.getType())
                .eq(req.getStatus() != null, MainDataManualSystem::getStatus, req.getStatus())
                .eq(req.getBusinessTypeId() != null, MainDataManualSystem::getBusinessTypeId, req.getBusinessTypeId())
                .eq(req.getBusinessTypeName() != null, MainDataManualSystem::getBusinessTypeName, req.getBusinessTypeName())
                .eq(MainDataManualSystem::getIsDeleted, IsDeletedCode.NO.getCode());
        IPage<MainDataManualSystem> page = newPage(req);
        IPage<MainDataManualSystem> mdms = mainDataManualSystemMapper.selectPage(page, qw);
        List<MainDataManualSystemDTO> result = EntityUtil.copyList(mdms.getRecords(), MainDataManualSystemDTO.class);
        PageResponse<MainDataManualSystemDTO> pageResponse = new PageResponse<>(result, page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() * page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
        return Result.success(pageResponse);
    }

    /**
     * 查询外挂系统主数据字典列表
     *
     * @param req
     * @return
     */
    @Override
    public Result<List<MainDataManualSystemDTO>> queryDictList(MainDataManualSystemQueryReq req) {
        QueryWrapper<MainDataManualSystem> qw = new QueryWrapper<>();
        qw.lambda().eq(req.getType() != null, MainDataManualSystem::getType, req.getType())
                .eq(req.getStatus() != null, MainDataManualSystem::getStatus, req.getStatus())
                .eq(MainDataManualSystem::getIsDeleted, IsDeletedCode.NO.getCode());
        List<MainDataManualSystem> list = mainDataManualSystemMapper.selectList(qw);
        List<MainDataManualSystemDTO> result = EntityUtil.copyList(list, MainDataManualSystemDTO.class);
        return newResult(result);
    }

    @Override
    public Result<Long> querySupplierIdById(Long id) {
        MainDataManualSystem mainDataManualSystemDTO = mainDataManualSystemMapper.selectById(id);
        if (mainDataManualSystemDTO == null || mainDataManualSystemDTO.getIsDeleted() == 1) {
            throw new BusinessException(SystemError.MAINDATA_4026);
        }
        return newResult(mainDataManualSystemDTO.getSupplierId());
    }

    /**
     * 根据id查询外挂系统主数据
     *
     * @param id
     * @return
     */
    @Override
    public Result<MainDataManualSystemDTO> findById(Long id) {
        validateNotNull(id);
        MainDataManualSystemDTO dto = mainDataManualSystemMapper.selectById(MainDataManualSystemDTO.class, id);
        return newResult(dto);
    }

    @Override
    public Result<List<MainDataBusinessTypeDTO>> queryBusinessTypeList() {
        List<MainDataBusinessTypeDTO> list = new ArrayList<>();
        for (SystemBusinessType systemBusinessType : SystemBusinessType.values()) {
            MainDataBusinessTypeDTO businessTypeDTO = new MainDataBusinessTypeDTO();
            businessTypeDTO.setBusinessTypeId(systemBusinessType.getCode());
            businessTypeDTO.setBusinessTypeName(systemBusinessType.getValue());
            list.add(businessTypeDTO);
        }
        return Result.success(list);
    }
}
